(function($) {
	$.fn.slide = function(options) {
		$.fn.slide.deflunt = {
			effect: "fade",
			autoPlay: false,
			delayTime: 500,
			interTime: 2500,
			defaultIndex: 0,
			titCell: ".hd li",
			mainCell: ".bd",
			trigger: "mouseover",
			scroll: 1,
			vis: 1,
			titOnClassName: "on",
			autoPage: false,
			prevCell: ".prev",
			nextCell: ".next"
		};
		return this.each(function() {
			var opts = $.extend({}, $.fn.slide.deflunt, options);
			var index = opts.defaultIndex;
			var prevBtn = $(opts.prevCell, $(this));
			var nextBtn = $(opts.nextCell, $(this));
			var navObj = $(opts.titCell, $(this));
			var navObjSize = navObj.size();
			var conBox = $(opts.mainCell, $(this));
			var conBoxSize = conBox.children().size();
			var slideH = 0;
			var slideW = 0;
			var selfW = 0;
			var selfH = 0;
			var autoPlay = opts.autoPlay;
			var inter = null;
			var oldIndex = index;
			if(conBoxSize < opts.vis) return;
			if(navObjSize == 0) navObjSize = conBoxSize;
			if(opts.autoPage) {
				var tempS = conBoxSize - opts.vis;
				navObjSize = 1 + parseInt(tempS % opts.scroll != 0 ? (tempS / opts.scroll + 1) : (tempS / opts.scroll));
				navObj.html("");
				for(var i = 0; i < navObjSize; i++) {
					navObj.append("<li>" + (i + 1) + "</li>")
				}
				var navObj = $("li", navObj);
			}
			conBox.children().each(function() {
				if($(this).width() > selfW) {
					selfW = $(this).width();
					slideW = $(this).outerWidth(true);
				}
				if($(this).height() > selfH) {
					selfH = $(this).height();
					slideH = $(this).outerHeight(true);
				}
			});
			switch(opts.effect) {
				case "top":
					conBox.wrap('<div class="tempWrap" style="overflow:hidden; position:relative; height:' + opts.vis * slideH + 'px"></div>').css({
						"position": "relative",
						"padding": "0",
						"margin": "0"
					}).children().css({
						"height": selfH
					});
					break;
				case "left":
					conBox.wrap('<div class="tempWrap" style="overflow:hidden; position:relative; width:' + opts.vis * slideW + 'px"></div>').css({
						"width": conBoxSize * slideW,
						"position": "relative",
						"overflow": "hidden",
						"padding": "0",
						"margin": "0"
					}).children().css({
						"float": "left",
						"width": selfW
					});
					break;
				case "leftLoop":
				case "leftMarquee":
					conBox.children().clone().appendTo(conBox).clone().prependTo(conBox);
					conBox.wrap('<div class="tempWrap" style="overflow:hidden; position:relative; width:' + opts.vis * slideW + 'px"></div>').css({
						"width": conBoxSize * slideW * 3,
						"position": "relative",
						"overflow": "hidden",
						"padding": "0",
						"margin": "0",
						"left": -conBoxSize * slideW
					}).children().css({
						"float": "left",
						"width": selfW
					});
					break;
				case "topLoop":
				case "topMarquee":
					conBox.children().clone().appendTo(conBox).clone().prependTo(conBox);
					conBox.wrap('<div class="tempWrap" style="overflow:hidden; position:relative; height:' + opts.vis * slideH + 'px"></div>').css({
						"height": conBoxSize * slideH * 3,
						"position": "relative",
						"padding": "0",
						"margin": "0",
						"top": -conBoxSize * slideH
					}).children().css({
						"height": selfH
					});
					break;
			}
			var doPlay = function() {
				switch(opts.effect) {
					case "fade":
					case "top":
					case "left":
						if(index >= navObjSize) {
							index = 0;
						} else if(index < 0) {
							index = navObjSize - 1;
						}
						break;
					case "leftMarquee":
					case "topMarquee":
						if(index >= 2) {
							index = 1;
						} else if(index < 0) {
							index = 0;
						}
						break;
					case "leftLoop":
					case "topLoop":
						var tempNum = index - oldIndex;
						if(navObjSize > 2 && tempNum == -(navObjSize - 1)) tempNum = 1;
						if(navObjSize > 2 && tempNum == (navObjSize - 1)) tempNum = -1;
						var scrollNum = Math.abs(tempNum * opts.scroll);
						if(index >= navObjSize) {
							index = 0;
						} else if(index < 0) {
							index = navObjSize - 1;
						}
						break;
				}
				switch(opts.effect) {
					case "fade":
						conBox.children().stop(true, true).eq(index).fadeIn(opts.delayTime).siblings().hide();
						break;
					case "top":
						conBox.stop(true, true).animate({
							"top": -index * opts.scroll * slideH
						}, opts.delayTime);
						break;
					case "left":
						conBox.stop(true, true).animate({
							"left": -index * opts.scroll * slideW
						}, opts.delayTime);
						break;
					case "leftLoop":
						if(tempNum < 0) {
							conBox.stop(true, true).animate({
								"left": -(conBoxSize - scrollNum) * slideW
							}, opts.delayTime, function() {
								for(var i = 0; i < scrollNum; i++) {
									conBox.children().last().prependTo(conBox);
								}
								conBox.css("left", -conBoxSize * slideW);
							});
						} else {
							conBox.stop(true, true).animate({
								"left": -(conBoxSize + scrollNum) * slideW
							}, opts.delayTime, function() {
								for(var i = 0; i < scrollNum; i++) {
									conBox.children().first().appendTo(conBox);
								}
								conBox.css("left", -conBoxSize * slideW);
							});
						}
						break;
					case "topLoop":
						if(tempNum < 0) {
							conBox.stop(true, true).animate({
								"top": -(conBoxSize - scrollNum) * slideH
							}, opts.delayTime, function() {
								for(var i = 0; i < scrollNum; i++) {
									conBox.children().last().prependTo(conBox);
								}
								conBox.css("top", -conBoxSize * slideH);
							});
						} else {
							conBox.stop(true, true).animate({
								"top": -(conBoxSize + scrollNum) * slideH
							}, opts.delayTime, function() {
								for(var i = 0; i < scrollNum; i++) {
									conBox.children().first().appendTo(conBox);
								}
								conBox.css("top", -conBoxSize * slideH);
							});
						}
						break;
					case "leftMarquee":
						var tempLeft = conBox.css("left").replace("px", "");
						if(index == 0) {
							conBox.animate({
								"left": ++tempLeft
							}, 0, function() {
								if(conBox.css("left").replace("px", "") >= 0) {
									for(var i = 0; i < conBoxSize; i++) {
										conBox.children().last().prependTo(conBox);
									}
									conBox.css("left", -conBoxSize * slideW);
								}
							});
						} else {
							conBox.animate({
								"left": --tempLeft
							}, 0, function() {
								if(conBox.css("left").replace("px", "") <= -conBoxSize * slideW * 2) {
									for(var i = 0; i < conBoxSize; i++) {
										conBox.children().first().appendTo(conBox);
									}
									conBox.css("left", -conBoxSize * slideW);
								}
							});
						}
						break;
					case "topMarquee":
						var tempTop = conBox.css("top").replace("px", "");
						if(index == 0) {
							conBox.animate({
								"top": ++tempTop
							}, 0, function() {
								if(conBox.css("top").replace("px", "") >= 0) {
									for(var i = 0; i < conBoxSize; i++) {
										conBox.children().last().prependTo(conBox);
									}
									conBox.css("top", -conBoxSize * slideH);
								}
							});
						} else {
							conBox.animate({
								"top": --tempTop
							}, 0, function() {
								if(conBox.css("top").replace("px", "") <= -conBoxSize * slideH * 2) {
									for(var i = 0; i < conBoxSize; i++) {
										conBox.children().first().appendTo(conBox);
									}
									conBox.css("top", -conBoxSize * slideH);
								}
							});
						}
						break;
				}
				navObj.removeClass(opts.titOnClassName).eq(index).addClass(opts.titOnClassName);
				oldIndex = index;
			};
			doPlay();
			if(autoPlay) {
				if(opts.effect == "leftMarquee" || opts.effect == "topMarquee") {
					index++;
					inter = setInterval(doPlay, opts.interTime);
					conBox.hover(function() {
						if(autoPlay) {
							clearInterval(inter);
						}
					}, function() {
						if(autoPlay) {
							clearInterval(inter);
							inter = setInterval(doPlay, opts.interTime);
						}
					});
				} else {
					inter = setInterval(function() {
						index++;
						doPlay()
					}, opts.interTime);
					$(this).hover(function() {
						if(autoPlay) {
							clearInterval(inter);
						}
					}, function() {
						if(autoPlay) {
							clearInterval(inter);
							inter = setInterval(function() {
								index++;
								doPlay()
							}, opts.interTime);
						}
					});
				}
			}
			var mst;
			if(opts.trigger == "mouseover") {
				navObj.hover(function() {
					clearTimeout(mst);
					index = navObj.index(this);
					mst = window.setTimeout(doPlay, 200);
				}, function() {
					if(!mst) clearTimeout(mst);
				});
			} else {
				navObj.click(function() {
					index = navObj.index(this);
					doPlay();
				})
			}
			nextBtn.click(function() {
				index++;
				doPlay();
			});
			prevBtn.click(function() {
				index--;
				doPlay();
			});
		});
	};
})(jQuery);;
(function($) {
	$.fn.keyWords = function() {
		this.focus(function() {
			if($(this).val() == '请输入关键字') {
				$(this).val('')
			}
			$(this).addClass('search_hover');
		}).blur(function() {
			var _t = $(this);
			if(_t.val() == '') {
				_t.val('请输入关键字').css('color', '#666');
			} else {
				_t.css('color', '#000')
			}
			_t.removeClass('search_hover');
		})
	}
})(jQuery);
$(function() {
	var _win = $(window);
	$('#button').click(function(e) {
		var pet_search_text = $('#keyword');
		if(pet_search_text.val() == '' || pet_search_text.val() == '请输入关键字') {
			pet_search_text.val('').focus();
			e.preventDefault();
		} else {
			$('#search_form').submit();
		}
	});
	$('.search_text').hover(function() {
		var _t = $(this);
		_t.find('.search_select').show().find('a').click(function() {
			$('#type').val($(this).attr('v'));
			_t.find('span').text($(this).text());
			_t.find('.search_select').hide();
		});
	}, function() {
		$(this).find('.search_select').hide();
	});
	$('.search').keyWords();
	$('.menu_list').hover(function() {
		$(this).find('p em').addClass('up');
		$(this).find('p').addClass('p_hover').next().show();
	}, function() {
		$(this).find('p em').removeClass('up');
		$(this).find('p').removeClass('p_hover').next().hide();
	});
	var goTop = $('.goTop');
	goTop.click(function() {
		$('html,body').animate({
			scrollTop: 0
		}, 200, "swing");
	});
	_win.bind('scroll', function() {
		if(_win.scrollTop() > 50) {
			goTop.show();
		} else {
			goTop.hide();
		}
	}).scroll();
	$('.rightside a').not($(".goTop")).bind('click', function() {
		var id = $(this).attr('href').substring(1);
		$('html,body').stop().animate({
			scrollTop: $('#' + id).offset().top
		}, 500, "swing");
		return false;
	});
});

function delayload() {
	var _src = '';
	var imgs = [];
	var _imgs = document.getElementsByTagName('img');
	for(var t = 0; t < _imgs.length; t++) {
		imgs.push(_imgs[t]);
	}
	var getLeft = function(El) {
		var left = 0;
		do {
			left += El.offsetLeft;
		} while ((El = El.offsetParent).nodeName != 'BODY');
		return left;
	};
	var getTop = function(El) {
		var top = 0;
		do {
			top += El.offsetTop;
		} while ((El = El.offsetParent).nodeName != 'BODY');
		return top;
	};
	var isIE = !!navigator.userAgent.match(/MSIE\b\s*([0-9]\.[0-9]);/img);
	isIE && (isIE = RegExp.$1);
	var isGoo = !!navigator.userAgent.match(/AppleWebKit\b/img);
	var box = isIE ? document.documentElement : document;

	function _scroll() {
		var top = isGoo ? document.body.scrollTop : document.documentElement.scrollTop,
			left = isGoo ? document.body.scrollLeft : document.documentElement.scrollLeft,
			width = document.documentElement.clientWidth,
			height = document.documentElement.clientHeight,
			halfW = width / 2,
			halfH = height / 2;
		for(var i = 0; i < imgs.length; i++) {
			var _top, _left;
			try {
				_top = getTop(imgs[i]), _left = getLeft(imgs[i]);
			} catch(e) {}
			if(_top != undefined) {
				if(_top >= top - halfH && _top - halfH <= top + height) {
					var _src = imgs[i].getAttribute('data-original');
					if(imgs[i].src != _src && _src != null) {
						console.log(_src);
						imgs[i].src = _src;
					}
				}
			}
		}
	};
	var onscroll = box.onscroll = _scroll;
	onscroll();
}

function CharMode(iN) {
	if(iN >= 48 && iN <= 57)
		return 1;
	if(iN >= 65 && iN <= 90)
		return 2;
	if(iN >= 97 && iN <= 122)
		return 4;
	else
		return 8;
}

function bitTotal(num) {
	modes = 0;
	for(i = 0; i < 4; i++) {
		if(num & 1) modes++;
		num >>>= 1;
	}
	return modes;
}

function checkStrong(sPW) {
	if(sPW.length <= 5)
		return 0;
	Modes = 0;
	for(i = 0; i < sPW.length; i++) {
		Modes |= CharMode(sPW.charCodeAt(i));
	}
	return bitTotal(Modes);
}

function toQzoneLogin() {
	var A = window.open("http://shop.boqii.com/api/qq/oauth/redirect_to_login.php", "TencentLogin", "width=590,height=460,menubar=0,scrollbars=1, resizable=1,status=1,titlebar=0,toolbar=0,location=1");
}

function AddFavorite(sURL, sTitle) {
	try {
		window.external.addFavorite(sURL, '波奇宠物网-www.boqii.com');
	} catch(e) {
		try {
			window.sidebar.addPanel(sTitle, sURL, "");
		} catch(e) {
			alert("加入收藏失败，请使用Ctrl+D进行添加");
		}
	}
}